package grape.base.rest.message.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.base.rest.message.form.MessageUserStateListPageForm;
import grape.base.rest.message.form.MessageUserStateUpdateForm;
import grape.base.rest.message.mapper.MessageUserStateWebMapper;
import grape.base.rest.message.mapper.MessageWebMapper;
import grape.base.rest.message.vo.MessageUserStateVo;
import grape.base.service.message.api.IMessageService;
import grape.base.service.message.api.IMessageUserStateService;
import grape.base.service.message.po.Message;
import grape.base.service.message.po.MessageUserState;
import grape.common.rest.mvc.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.bridge.IMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
/**
 * <p>
 * 用户消息读取状态表 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-03-18
 */
@RestController
@RequestMapping("/base/messageuserstate")
@Api(tags = "用户消息读取状态相关接口")
public class MessageUserStateController extends BaseController<MessageUserStateVo, MessageUserState> {

    @Autowired
    private MessageUserStateWebMapper currentWebMapper;
    @Autowired
    private IMessageUserStateService currentService;
    @Autowired
    private IMessageService messageService;
    @Autowired
    private MessageWebMapper messageWebMapper;


     @ApiOperation("根据ID查询用户消息读取状态")
     @PreAuthorize("hasAuthority('messageuserstate:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public MessageUserStateVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除用户消息读取状态")
     @PreAuthorize("hasAuthority('messageuserstate:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新用户消息读取状态")
     @PreAuthorize("hasAuthority('messageuserstate:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public MessageUserStateVo update(@PathVariable String id,@RequestBody @Valid MessageUserStateUpdateForm uf) {
         MessageUserState po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("分页查询用户消息读取状态")
    @PreAuthorize("hasAuthority('messageuserstate:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<MessageUserStateVo> listPage(MessageUserStateListPageForm listPageForm) {
         MessageUserState po = currentWebMapper.formToPo(listPageForm);
        return super.listPage(po, listPageForm);
    }

    @Override
    public MessageUserStateVo transVo(MessageUserStateVo messageUserStateVo) {
        Message message = messageService.getById(messageUserStateVo.getMessageId());
        messageUserStateVo.setMessage(messageWebMapper.poToVo(message));
        return super.transVo(messageUserStateVo);
    }
}
